import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:management/model/visitor_model.dart';
import 'package:management/utils/convert.dart';

///按时间删除来访记录
class DeleteRecordByTime extends StatefulWidget {
  @override
  _DeleteRecordByTimeState createState() => _DeleteRecordByTimeState();
}

class _DeleteRecordByTimeState extends State<DeleteRecordByTime> {
  TextEditingController _startTimeController = TextEditingController();
  TextEditingController _endTimeController = TextEditingController();

  @override
  void initState() {
    SystemChrome.setEnabledSystemUIOverlays([]);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        // title: Text(""),
        leading: IconButton(
          icon: Icon(
            Icons.arrow_back,
            size: 35,
          ),
          onPressed: () {
            // Fluttertoast.showToast(msg: "返回");
            // Navigator.of(context)
            //     .popUntil(ModalRoute.withName('/VisitorRecordQuery'));
            // Navigator.of(context).pushNamed('/QueryResult');
            Navigator.pop(context);
          },
        ),
      ),
      body: Container(
        margin: EdgeInsets.all(20),
        height: 250,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Container(
              child: Row(
                children: [
                  Text(
                    "开始日期:",
                    style: TextStyle(
                      fontSize: 25,
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(left: 20),
                    width: 300,
                    child: TextField(
                      controller: _startTimeController,
                      readOnly: true,
                      decoration: InputDecoration(
                        hintText: "请选择开始日期",
                        border: OutlineInputBorder(),
                      ),
                      onTap: () async {
                        DateTimeRange? dateTimeRange = await showDateRangePicker(
                          context: context,
                          firstDate: DateTime(2020),
                          lastDate: DateTime.now(),
                          currentDate: DateTime.now(),
                        );
                        String startTime = Convert.format(dateTimeRange!.start);
                        String endTime = Convert.format(dateTimeRange.end);
                        _startTimeController.text = startTime;
                        _endTimeController.text = endTime;
                      },
                    ),
                  ),
                ],
              ),
            ),
            Container(
              child: Row(
                children: [
                  Text(
                    "结束日期:",
                    style: TextStyle(
                      fontSize: 25,
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(left: 20),
                    width: 300,
                    child: TextField(
                      controller: _endTimeController,
                      readOnly: true,
                      decoration: InputDecoration(
                          hintText: "请选择结束日期", border: OutlineInputBorder()),
                      onTap: () async {
                        DateTimeRange? dateTimeRange = await showDateRangePicker(
                          context: context,
                          firstDate: DateTime(2020),
                          lastDate: DateTime.now(),
                          currentDate: DateTime.now(),
                        );
                        if (dateTimeRange != null) {
                          String startTime =
                              Convert.format(dateTimeRange.start);
                          String endTime = Convert.format(dateTimeRange.end);
                          _startTimeController.text = startTime;
                          _endTimeController.text = endTime;
                        }
                      },
                    ),
                  ),
                ],
              ),
            ),
            MaterialButton(
              height: 50,
              minWidth: 300,
              child: Text(
                "删除该时间段内的记录",
                style: TextStyle(color: Colors.white, fontSize: 20),
              ),
              color: Colors.blue,
              onPressed: () {
                if (_startTimeController.text.isEmpty) {
                  return;
                }
                if (_endTimeController.text.isEmpty) {
                  return;
                }
                showDialog(
                    context: context,
                    builder: (context) => AlertDialog(
                          title: Text("提示"),
                          content: Text(
                            "确定要这样做吗？",
                            style: TextStyle(
                              color: Colors.red,
                            ),
                          ),
                          actions: [
                            TextButton(
                              onPressed: () {
                                VisitorProvider.provider.deleteByTime(
                                    _startTimeController.text,
                                    _endTimeController.text);

                                Navigator.of(context).pop();
                                Fluttertoast.showToast(msg: "删除成功");
                              },
                              child: Text("确定"),
                            ),
                            TextButton(
                              onPressed: () {
                                Navigator.of(context).pop();
                              },
                              child: Text("取消"),
                            ),
                          ],
                        ));
              },
            ),
          ],
        ),
      ),
    );
  }
}
